<template>
<dic style="background-color:#fff;padding:10px 20px;">
  <sapn style="line-height:60px;font-size:20px;">字典项管理</sapn>
  <el-button @click="goBack" type="primary" style="float:right;margin-top:13px;">返回</el-button>
</dic>
  <el-card style="margin:5px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="字典项名称">
        <el-input  v-model="searchDictOption.label" placeholder="请输入" @keydown.enter.prevent="loadDictOption"></el-input>
      </el-form-item>
      <el-form-item label="字典项值">
        <el-input v-model="searchDictOption.value" placeholder="请输入" @keydown.enter.prevent="loadDictOption"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="reset">重置</el-button>
        <el-button @click="loadDictOption" type="primary">搜索</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <el-card style="margin:5px;">
    <div style="margin:10px 0px 40px 10px;">
      <span style="font-weight: bold;">{{dictName}}</span>的字典项表
      <el-button @click="addDictOption" type="primary" style="float: right;margin-top:-3px;">新增字典项</el-button>
    </div>
    <el-table :data="dictOptionArr" style="width: 100%">
      <el-table-column type="index" label="编号" width="80" align="center"></el-table-column>
      <el-table-column prop="label" label="字典项名称" align="center"></el-table-column>
      <el-table-column prop="value" label="字典项值" align="center"/>
      <el-table-column prop="sort" label="字典项排序" align="center"/>
      <el-table-column prop="remark" label="备注信息" align="center"/>
      <el-table-column prop="createTime" label="创建时间" align="center"/>
      <el-table-column prop="updateTime" label="更新时间" align="center"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" link @click="editDictOption(scope.row)">编辑</el-button>
          <el-button type="primary" link @click="deleteDictOption(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <el-dialog :title="dialogTitle" v-model="dialogVisible" style="width:1000px;padding:40px;"
             :close-on-click-modal="false">
    <el-form label-width="80px" label-position="top">
      <el-form-item label="字典项名称">
        <el-input placeholder="请输入" v-model="dictOption.label"></el-input>
      </el-form-item>
      <el-form-item label="字典项值">
        <el-input placeholder="请输入" v-model="dictOption.value"></el-input>
      </el-form-item>
      <el-form-item label="字典项排序">
        <el-input placeholder="请输入" v-model="dictOption.sort"></el-input>
      </el-form-item>
      <el-form-item label="备注信息">
        <el-input placeholder="请输入" v-model="dictOption.remark"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveDictOption">保存</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import router from "@/router";
import {onMounted, ref} from "vue";
import axios from "axios";
import {ElMessage} from "element-plus";

const goBack = () => {
  router.push("/empHome/dict")
}
const dictId=new URLSearchParams(window.location.search).get("id")
const dictName =new URLSearchParams(window.location.search).get("name")
const searchDictOption=ref({dictId:dictId})
const reset = () => {
  searchDictOption.value={dictId:dictId}
  loadDictOption()
}
const dictOptionArr=ref([])
const loadDictOption = () => {
  axios.post(BASE_URL+'/v1/dictOption/select',searchDictOption.value)
      .then((response)=>{
        if (response.data.code==2000){
          console.log(response.data.data)
          dictOptionArr.value=response.data.data
        }else {
          ElMessage.error(response.data.msg)
        }
      })
}
onMounted(()=>loadDictOption())
const dialogTitle=ref("")
const dialogVisible=ref(false)
const dictOption=ref({})
const handleClose = () => {
  if (confirm("确定关闭弹窗吗")){
    dialogVisible.value=false
  }
}
const addDictOption = () => {
  dialogTitle.value="新建字典项"
  dialogVisible.value=true
  dictOption.value={}
}
const editDictOption = (row) => {
  dialogTitle.value="编辑字典项"
  dialogVisible.value=true
  loadDictOption()
  dictOption.value=row
}
const saveDictOption = () => {
  if (confirm("确认保存吗")){
    dictOption.value.dictId=dictId
    axios.post(BASE_URL+'/v1/dictOption/save',dictOption.value)
        .then((response)=>{
          if (response.data.code==2000){
            ElMessage.success("保存成功")
            dictOption.value={}
            dialogVisible.value=false
            loadDictOption()
          }else {
            ElMessage.error(response.data.msg)
          }
        })
  }
}
const deleteDictOption = (id) => {
  let i=parseInt(dictId)
  let del={id:id,dictId:i}
  console.log(del)
  if (confirm("确认要删除吗")){
    axios.delete(BASE_URL+'/v1/dictOption/delete',{data:del})
        .then((response)=>{
          if (response.data.code==2000){
            ElMessage.success("删除成功")
            loadDictOption()
          }else {
            ElMessage.error(response.data.msg)
          }
        })
  }
}
</script>

<style scoped>

</style>